package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private CommentApi commentApi;

    @DubboReference
    private AnnouncementsApi announcementApi;

    /**
     * 通过环信id查询用户数据
     *
     * @param huanxinId
     * @return
     */
    public UserInfoVo userInfo(String huanxinId) {
        //1.根据环信id查询用户
        User user = userApi.findByHuanXin(huanxinId);
        if (user == null) {
            return null;
        }
        //2.根据用户id查询用户详细数据
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //3.封装vo返回
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    /**
     * 添加联系人
     *
     * @param friendId
     */
    public void contacts(Long friendId) {
        //1.将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + friendId,
                Constants.HX_USER_PREFIX + UserHolder.getUserId());
        if (aBoolean) {
            //注册成功
            //2.调用api添加好友关系到好友关系表
            friendApi.save(friendId, UserHolder.getUserId());
        }
    }

    /**
     * 查询联系人列表
     *
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.调用api查询当前用户的好友关系数据
        List<Friend> list = friendApi.findFriends(UserHolder.getUserId(), page, pagesize);
        //2.从好友关系数据中获取所有好友id
        List<Long> friendIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3.调用userInfoaApi查询所有好友的详细信息
        //3.1构造查询条件
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(friendIds, userInfo);
        //4.构造返回vo
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo info = userInfoMap.get(friend.getFriendId());
            //4.1一个好友构造一个vo对象
            if (info != null) {
                ContactVo vo = ContactVo.init(info);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0L, vos);
    }

    /**
     * 点赞列表
     * likes路径参数
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult likes(Integer page, Integer pagesize, String likes) {
        List<Comment> list = null;
        //1.调用api查询所有给当前用户点赞的数据
        if (likes.equals("likes")) {
            list = commentApi.likes(UserHolder.getUserId(), page, pagesize, CommentType.LIKE);
        } else if (likes.equals("loves")) {
            list = commentApi.likes(UserHolder.getUserId(), page, pagesize, CommentType.LOVE);
        } else if (likes.equals("comments")) {
            list = commentApi.likes(UserHolder.getUserId(), page, pagesize, CommentType.COMMENT);
        }
        //2.判断是否为空
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //3.通过获取到的list集合数据得到每一个评论人的id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //4.通过id获取到每个用户的详细信息
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        //5.组装vo返回
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            //遍历循环，通过评论人id获取到一个用户详细信息
            UserInfo userInfo = infoMap.get(comment.getUserId());
            //一个comment组装一个vo对象
            CommentVo vo = CommentVo.init(userInfo, comment);
            vos.add(vo);
        }
        return new PageResult(page, pagesize, 0L, vos);
    }

    /**
     * 公告
     *
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult announcements(Integer page, Integer pagesize) {
        List<Announcement> list = announcementApi.announcement(page,pagesize);
        return new PageResult(page, pagesize, Long.valueOf(list.size()), list);
    }
}
